/*
 * Copyright (c) 2016 4PX Information Technology Co.,Ltd. All rights reserved.
 */
package com.open.spring.utils.soap.header;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;

import org.springframework.ws.soap.SoapMessageCreationException;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;

/**
 * spring ws无法添加header的bug
 * 
 * @author hcx
 * @date 2017年6月22日
 */
public class SaajSoapMessageFactoryEx extends SaajSoapMessageFactory {
    private MessageFactory messageFactory;

    private SOAPMessage soapMessage;

    private boolean langAttributeOnSoap11FaultString = true;

    public SaajSoapMessageFactoryEx(MessageFactory messageFactory) {
        super(messageFactory);
        this.messageFactory = messageFactory;
    }

    @Override
    public SaajSoapMessage createWebServiceMessage() {
        try {
            postProcess(getSoapMessage());
            return new SaajSoapMessage(getSoapMessage(), langAttributeOnSoap11FaultString, messageFactory);
        } catch (SOAPException ex) {
            throw new SoapMessageCreationException("Could not create empty message: " + ex.getMessage(), ex);
        }
    }

    /**
     * @return the langAttributeOnSoap11FaultString
     */
    public boolean isLangAttributeOnSoap11FaultString() {
        return langAttributeOnSoap11FaultString;
    }

    /**
     * @param langAttributeOnSoap11FaultString the langAttributeOnSoap11FaultString to set
     */
    public void setLangAttributeOnSoap11FaultString(boolean langAttributeOnSoap11FaultString) {
        this.langAttributeOnSoap11FaultString = langAttributeOnSoap11FaultString;
    }

    /**
     * @return the soapMessage
     */
    public SOAPMessage getSoapMessage() {
        if (null == soapMessage) {
            try {
                this.soapMessage = messageFactory.createMessage();
            } catch (SOAPException ex) {
                throw new SoapMessageCreationException("Could not create empty message: " + ex.getMessage(), ex);
            }

        }
        return soapMessage;
    }

    /**
     * @param soapMessage the soapMessage to set
     */
    public void setSoapMessage(SOAPMessage soapMessage) {
        this.soapMessage = soapMessage;
    }

}
